from typing import Union

from PyQt6.QtCore import Qt, QUrl, QObject
from PyQt6.QtGui import QPainter, QColor
from PyQt6.QtWidgets import QHBoxLayout, QApplication
from qfluentwidgets import CaptionLabel, FluentIcon as FIF, isDarkTheme
from qfluentwidgets.multimedia import MediaPlayer
from qfluentwidgets.multimedia.media_play_bar import MediaPlayBarBase, MediaPlayBarButton

from common import Icon


class SimpleMediaPlayBar(MediaPlayBarBase):
    """ 简易媒体播放 """

    def __init__(self, parent=None):
        super().__init__(parent)
        self.hBoxLayout = QHBoxLayout(self)
        self.previousButton = MediaPlayBarButton(Icon.PREVIOUS, self)
        self.nextButton = MediaPlayBarButton(Icon.NEXT, self)
        self.currentTimeLabel = CaptionLabel('0:00:00', self)
        self.remainTimeLabel = CaptionLabel('0:00:00', self)
        self.fullScreenButton = MediaPlayBarButton(FIF.FULL_SCREEN, self)
        self.player = MediaPlayer(self)

        self.__initWidgets()

    def __initWidgets(self):
        self.previousButton.setToolTip('上一个')
        self.nextButton.setToolTip('下一个')
        self.fullScreenButton.setToolTip('全屏')

        self.hBoxLayout.setContentsMargins(10, 4, 10, 4)
        self.hBoxLayout.setSpacing(6)
        self.hBoxLayout.addWidget(self.previousButton, 0, Qt.AlignmentFlag.AlignLeft)
        self.hBoxLayout.addWidget(self.playButton, 0, Qt.AlignmentFlag.AlignLeft)
        self.hBoxLayout.addWidget(self.nextButton, 0, Qt.AlignmentFlag.AlignLeft)
        self.hBoxLayout.addWidget(self.currentTimeLabel, 0)
        self.hBoxLayout.addWidget(self.progressSlider, 1)
        self.hBoxLayout.addWidget(self.remainTimeLabel, 0)
        self.hBoxLayout.addWidget(self.volumeButton, 0)
        self.hBoxLayout.addWidget(self.fullScreenButton, 0, Qt.AlignmentFlag.AlignRight)

        self.setFixedHeight(48)
        self.setMediaPlayer(self.player)

    def addButton(self, button: MediaPlayBarButton):
        """ 在播放栏右侧添加按钮 """
        self.hBoxLayout.addWidget(button, 0)

    def setVideoOutput(self, widget: QObject):
        self.player.setVideoOutput(widget)

    def _onPositionChanged(self, position: int):
        super()._onPositionChanged(position)
        self.currentTimeLabel.setText(self._formatTime(position))
        self.remainTimeLabel.setText(self._formatTime(self.player.duration() - position))

    def setPreviousVisible(self, visible: bool):
        self.previousButton.setVisible(visible)

    def setNextVisible(self, visible: bool):
        self.nextButton.setVisible(visible)

    def setSource(self, url: Union[str, QUrl]):
        self.player.setSource(QUrl(url))

    def _formatTime(self, time: int):
        time = int(time / 1000)
        s = time % 60
        m = int(time / 60)
        h = int(time / 3600)
        return f'{h}:{m:02}:{s:02}'

    def paintEvent(self, e):
        painter = QPainter(self)
        painter.setRenderHints(QPainter.RenderHint.Antialiasing)

        if isDarkTheme():
            painter.setBrush(QColor(46, 46, 46))
            painter.setPen(QColor(0, 0, 0, 20))
        else:
            painter.setBrush(QColor(248, 248, 248))
            painter.setPen(QColor(0, 0, 0, 10))

        painter.drawRoundedRect(self.rect().adjusted(1, 1, -1, -1), 8, 8)


if __name__ == '__main__':
    app = QApplication([])
    widget = SimpleMediaPlayBar()
    widget.show()
    app.exec()
